import { observable, action, computed, runInAction, toJS } from 'mobx';

import BaseStore from './BaseStore';

class AuthResource extends BaseStore {
    // 具有权限的
    @observable _authedResources = [];

    @computed
    get authedResources() {
        return toJS(this._authedResources) || [];
    }

    @action
    init() {
        this._authedResources = [];
    }

    @action
    async verifyAuthResources(resources) {
        if (!resources || resources.length < 1) {
            return;
        }
        const authedResources = this.authedResources;
        if (authedResources.length > 0) {
            resources = resources.filter(item => authedResources.indexOf(item) === -1);
        }
        if (resources.length < 1) {
            return;
        }
        const res = await this.get('/auth/resources-auth', {resource_key: resources.join(',')});
        runInAction(() => {
            if (res && res.code === '0000') {
                this._authedResources = [
                    ...authedResources,
                    ...res.data,
                ];
            }
        });
    }
}
export default new AuthResource();
